require 'fileutils'
require 'qiniu'
require 'digest'
require 'openssl'
require 'base64'
require 'time'
class EventApi < Grape::API
	#通用接口
	version 'v1', using: :path
	format :json
	default_format :json
	formatter :json, Grape::Formatter::Rabl
	resource :event do
		
		desc '待处理列表'
    params do
			optional :user_name, type: String, desc: '登录用户名(手机号/身份证号)'
			optional :passwd, type: String, desc: '密码'
			optional :project_id, type: String, desc: '项目ID'
			optional :page, type: Integer, desc: '页码'
    end
		get 'events' do
			if params[:user_name].nil? && params[:passwd].nil?
				{code: "400", msg: "用户名和密码缺失"}
			else
				user_id = UserAPI.user_login(params[:user_name], params[:passwd])
				if user_id == -1
					{code: "400", msg: "用户名和密码错误"}
				else
					per_page = 10
					if params[:page].nil? || params[:page] == 1
						result = Hash.new
						result["code"] = "0"
						result["msg"] = "OK"
						if params[:project_id].nil?
							events = Event.where(confirm_status: 1, user_id: user_id).group("module_id").order(created_at: :desc).limit(per_page)
							event_count = Event.where(confirm_status: 1, user_id: user_id).group("module_id").order(created_at: :desc).count
							event_list = EventApi.build_event(events)
						else
							events = Event.where(confirm_status: 1, user_id: user_id, project_id: params[:project_id]).group("module_id").order(created_at: :desc).limit(per_page)
							event_count = Event.where(confirm_status: 1, user_id: user_id).group("module_id").order(created_at: :desc).count
							event_list = EventApi.build_event(events)
						end
						result["events"] = event_list
						result["count"] = events.length
						result["previous_page"] = ""
						if (event_count.length) > per_page
							result["next_page"] = "2"
						else
							result["next_page"] = ""
						end
						return result
					else
						offset = (params[:page].to_i - 1) * per_page
						result = Hash.new
						result["code"] = "0"
						result["msg"] = "OK"
						if params[:project_id].nil?
							events = Event.where(confirm_status: 1, user_id: user_id).group("module_id").order(created_at: :desc).limit(per_page).offset(offset)
							event_count = Event.where(confirm_status: 1, user_id: user_id).group("module_id").order(created_at: :desc).count
							event_list = EventApi.build_event(events)
						else
							events = Event.where(confirm_status: 1, user_id: user_id, project_id: params[:project_id]).group("module_id").order(created_at: :desc).limit(per_page).offset(offset)
							event_count = Event.where(confirm_status: 1, user_id: user_id).group("module_id").order(created_at: :desc).offset(offset).count
							event_list = EventApi.build_event(events)
						end
						result["events"] = event_list
						result["count"] = events.length
						result["previous_page"] = (params[:page].to_i - 1).to_s
						if (event_count.length) > (params[:page].to_i * per_page)
							result["next_page"] = params[:page].to_i + 1
						else
							result["next_page"] = ""
						end
						return result
					end
				end
			end
		end
		def self.make_event(user_id, project_id, module_type, module_id)
			event = Event.new
			event.user_id = user_id
			event.project_id = project_id
			event.module_type = module_type
			event.module_id = module_id
			event.confirm_status = 1
			event.created_at = Time.now
			event.updated_at = Time.now
			if event.save
				return event.id
			else
				return -1
			end
		end
		def self.process_event(event_id)
			event = Event.where(id: event_id).first
			event.confirm_status = 2
			event.updated_at = Time.now
			event.save
		end
		def self.build_event(events)
			event_list = []
			events.each do |e|
				if e.module_type == 1
					event = Hash.new
					work = Work.where(id: e.module_id).first
					project = Project.where(id: work.project_id).first
					event["project_id"] = project.id
					event["project_name"] = project.name
					event["event_type"] = 1
					event["publish_user"] = work.publish_user
					event["work_id"] = work.id
					event["work"] = work.content
					event["publish_time"] = work.created_at.strftime("%Y-%m-%d %H:%M")
					event_list << event
				elsif e.module_type == 2
					event = Hash.new
					work = Work.where(id: e.module_id).first
					project = Project.where(id: work.project_id).first
					event["project_id"] = project.id
					event["project_name"] = project.name
					event["event_type"] = 2
					event["publish_user"] = work.publish_user
					event["work_id"] = work.id
					event["work"] = work.content
					event["publish_time"] = work.created_at.strftime("%Y-%m-%d %H:%M")
					event_list << event
				elsif e.module_type ==3
					event = Hash.new
					evaluation = Evaluation.where(id: e.module_id).first
					project = Project.where(id: evaluation.project_id).first
					event["project_id"] = project.id
					event["project_name"] = project.name
					event["event_type"] = 3
					event["publish_user"] = evaluation.publish_user
					event["evaluation_id"] = evaluation.id
					event["score"] = evaluation.score
					event["publish_time"] = evaluation.created_at.strftime("%Y-%m-%d %H:%M")
					event_list << event
				end
			end
			return event_list
		end
	end
end
